package com.colin.app.activity.senior;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.app.activity.main.MainActivity;
import com.colin.app.app.AppActivity;
import com.colin.app.app.BasePresenterImpl;
import com.colin.app.bean.DemoBean;
import com.colin.app.bean.ItemBean;
import com.colin.library.help.TimeHelp;
import com.colin.library.help.ToastHelp;
import com.colin.library.other.Constants;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * Created by Colin on 2017/2/7.
 */

public class SeniorPresenterImpl extends BasePresenterImpl implements SeniorContract.SeniorPresenter {

    private SeniorContract.SeniorView mSeniorView;
    private SeniorContract.SeniorModel mSeniorModel;

    public SeniorPresenterImpl(AppActivity mAppActivity, SeniorContract.SeniorView mSeniorView, DemoBean demoBean) {
        super(mAppActivity, mSeniorView);
        this.mSeniorView = mSeniorView;
        this.mSeniorModel = new SeniorModelImpl(demoBean);
    }

    @Override
    public void setData() {
        mSeniorView.setTitle(mSeniorModel.getTitle());
    }

    @Override
    public void animationAlpha(View view, TextView text_animation_name) {
        view.startAnimation(mSeniorModel.getAlphaAnimation());
        text_animation_name.setText(R.string.button_animation_alpha);
    }

    @Override
    public void animationRotate(View view, TextView text_animation_name) {
        view.startAnimation(mSeniorModel.getRotateAnimation());
        text_animation_name.setText(R.string.button_animation_rotate);
    }

    @Override
    public void animationRotateSelf(View view, TextView text_animation_name) {
        view.startAnimation(mSeniorModel.getRotateAnimationSelf());
        text_animation_name.setText(R.string.button_animation_rotate_self);
    }

    @Override
    public void animationTranslate(View view, TextView text_animation_name) {
        view.startAnimation(mSeniorModel.getTranslateAnimation());
        text_animation_name.setText(R.string.button_animation_translate);
    }

    @Override
    public void animationScale(View view, TextView text_animation_name) {
        view.startAnimation(mSeniorModel.getScaleAnimation());
        text_animation_name.setText(R.string.button_animation_scale);
    }

    @Override
    public void animationScaleSelf(View view, TextView text_animation_name) {
        view.startAnimation(mSeniorModel.getScaleAnimationSelf());
        text_animation_name.setText(R.string.button_animation_scale_self);
    }

    @Override
    public void animationSet(View view, TextView text_animation_name) {
        view.startAnimation(mSeniorModel.getAnimationSet());
        text_animation_name.setText(R.string.button_animation_set);
    }

    @Override
    public void animationPropertyValuesHolder(View view, TextView text_animation_name) {
        text_animation_name.setText(R.string.button_animation_property_values_holder);
        List<PropertyValuesHolder> propertyValuesHolderList = mSeniorModel.getAnimationPropertyValuesHolderList();
        if (null != propertyValuesHolderList && propertyValuesHolderList.size() == 3) {
            ObjectAnimator.ofPropertyValuesHolder(view
                    , propertyValuesHolderList.get(0)
                    , propertyValuesHolderList.get(1)
                    , propertyValuesHolderList.get(2))
                    .setDuration(Constants.ANIMATION_DURATION_TIME).start();
        }
    }

    @Override
    public void animationSetCopyHolder(View view, TextView text_animation_name) {
        mSeniorModel.getAnimationSetCopyHolder(view).start();
        text_animation_name.setText(R.string.button_animation_set_holder);
    }

    @Override
    public void animationValues(View view, TextView text_animation_name) {
        text_animation_name.setText(R.string.button_animation_values);
        ValueAnimator valueAnimator = mSeniorModel.getValuesAnimation();
        valueAnimator.setTarget(view);
        valueAnimator.start();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Float value = (Float) animation.getAnimatedValue();
                ToastHelp.showShort("value==" + String.valueOf(value));
            }
        });
    }

    @Override
    public void animationXML(View view, TextView text_animation_name) {
        text_animation_name.setText(R.string.button_animation_rotate);
        Animator animator = AnimatorInflater.loadAnimator(mAppActivity, R.animator.scalex);
        animator.setTarget(view);
        animator.start();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void notifyBasic() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
        // 构造PendingIntent，需要在普通的Intent上面包装一层
        PendingIntent pendingIntent = PendingIntent.getActivity(mAppActivity, 0, intent, 0);
        // 创建Notification对象
        Notification.Builder builder = new Notification.Builder(mAppActivity);
        // 设置Notification的各种属性

        // PS：没有设置smallIcon的时候，不会产生Notification，但是不设置LargeIcon是可以产生Notification的
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(pendingIntent);
        //点击之后自动消失
        builder.setAutoCancel(true);
        builder.setLargeIcon(BitmapFactory.decodeResource(mAppActivity.getResources(), R.mipmap.ic_launcher));
        builder.setContentTitle(mAppActivity.getString(R.string.button_notify_basic));
        builder.setContentText("I am a basic notification");
        builder.setSubText("it is really basic");
        // 背景颜色
        builder.setColor(Color.GREEN);
        // 通过NotificationManager来发出Notification
        NotificationManager notificationManager = (NotificationManager) mAppActivity.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(Constants.NOTIFICATION_ID_BASIC, builder.build());
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void collapsedNotify() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
        // 构造PendingIntent，需要在普通的Intent上面包装一层
        PendingIntent pendingIntent = PendingIntent.getActivity(mAppActivity, 0, intent, 0);
        // 创建Notification对象
        Notification.Builder builder = new Notification.Builder(mAppActivity);
        // 设置Notification的各种属性
        // PS：没有设置smallIcon的时候，不会产生Notification，但是不设置LargeIcon是可以产生Notification的
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);
        builder.setLargeIcon(BitmapFactory.decodeResource(mAppActivity.getResources(), R.mipmap.tsp_login_bg));
        // 通过RemoteViews建立自定义的Notification视图
        RemoteViews contentView = new RemoteViews(mAppActivity.getPackageName(), R.layout.layout_notification);
        contentView.setTextViewText(R.id.text_notification_title, "Show me when collapsed");
        Notification notification = builder.build();
        // 指定视图
        notification.contentView = contentView;
        // 通过RemoteViews建立自定义的Notification视图——下拉时候的大视图
        RemoteViews expandedView = new RemoteViews(mAppActivity.getPackageName(), R.layout.layout_notification_expanded);
        // 指定视图
        notification.bigContentView = expandedView;
        // 通过NotificationManager来发出Notification
        NotificationManager notificationManager = (NotificationManager) mAppActivity.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(Constants.NOTIFICATION_ID_COLLAPSE, notification);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void notifyHeadsUp() {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(mAppActivity, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(mAppActivity, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        Notification.Builder builder = new Notification.Builder(mAppActivity)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setCategory(Notification.CATEGORY_MESSAGE)
                .setContentTitle(mAppActivity.getString(R.string.button_notify_heads_up))
                .setContentText("I am HeadsUp Notification.")
                .setFullScreenIntent(pendingIntent, true);
        NotificationManager notificationManager = (NotificationManager) mAppActivity.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(Constants.NOTIFICATION_ID_HEADS_UP, builder.build());
    }

    @Override
    public void notifyBase() {
        //初始化意图，传递值，跳转界面
        Intent intent = new Intent(mAppActivity, MainActivity.class);
        //开启通知服务:Notification管理
        NotificationManager notificationManager = (NotificationManager) mAppActivity.getSystemService(NOTIFICATION_SERVICE);
        //初始化通知特定的意图
        PendingIntent pendingIntent = PendingIntent.getActivity(mAppActivity, Constants.NOTIFICATION_ID_BASE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //Notification构造器
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mAppActivity);
        mBuilder.setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.login_user)
                .setLargeIcon(BitmapFactory.decodeResource(mAppActivity.getResources(), R.mipmap.ic_launcher))
                .setContentTitle(mAppActivity.getString(R.string.button_notify_base))
                .setContentText("测试内容:"+ TimeHelp.getCurrentlyTime())
                .setNumber(5)//显示数量
                .setTicker("测试通知来啦")//通知首次出现在通知栏，带上升动画效果的
                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示
                .setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级
                .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
                .setOngoing(false)//ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
//                .setVibrate(new long[]{0, 1000, 1000, 1000})//自定义震动效果
//                .setLights(Color.GREEN,1000,1000)//LED灯
                .setDefaults(Notification.DEFAULT_ALL);//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合：
        //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
        //显示
        notificationManager.notify(Constants.NOTIFICATION_ID_BASE, mBuilder.build());

    }

    @Override
    public void notifyBigStyle() {
        //开启通知服务:Notification管理
        NotificationManager notificationManager = (NotificationManager) mAppActivity.getSystemService(NOTIFICATION_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getActivity(mAppActivity, 1, new Intent(), Notification.FLAG_AUTO_CANCEL);
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        String[] events = new String[5];
        // Sets a title for the Inbox style big view
        inboxStyle.setBigContentTitle("大视图内容:");
        // Moves events into the big view
        for (int i = 0; i < events.length; i++) {
            inboxStyle.addLine(events[i]);
        }
        //Notification构造器
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mAppActivity);
        mBuilder.setContentIntent(pendingIntent)
                .setContentTitle(mAppActivity.getString(R.string.button_notify_big_style))
                .setContentText("测试内容")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setNumber(5)//显示数量
                .setStyle(inboxStyle)//设置风格
                .setTicker("测试通知来啦")//通知首次出现在通知栏，带上升动画效果的
                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示
                .setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级
                .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
                .setOngoing(false)//ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                .setDefaults(Notification.DEFAULT_VIBRATE);//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合：
//                Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
        //显示
        notificationManager.notify(Constants.NOTIFICATION_ID_BIG_STYLE, mBuilder.build());
    }

    @Override
    public void notifyLong() {
        //开启通知服务:Notification管理
        NotificationManager notificationManager = (NotificationManager) mAppActivity.getSystemService(NOTIFICATION_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getActivity(mAppActivity, 1, new Intent(), 0);
        //Notification构造器
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mAppActivity);
        mBuilder.setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(mAppActivity.getString(R.string.button_notify_long))
                .setContentText("使用cancel()方法才可以把我去掉哦")
                .setNumber(5)//显示数量
                .setTicker("常驻通知来了")//通知首次出现在通知栏，带上升动画效果的
                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示
                .setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级
                .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
                .setOngoing(false)//ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                .setDefaults(Notification.DEFAULT_VIBRATE);//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合：
//                Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission


        Notification mNotification = mBuilder.build();
        //设置通知  消息  图标
        mNotification.icon = R.mipmap.ic_launcher;
        //在通知栏上点击此通知后自动清除此通知
        mNotification.flags = Notification.FLAG_ONGOING_EVENT;//FLAG_ONGOING_EVENT 在顶部常驻，可以调用下面的清除方法去除  FLAG_AUTO_CANCEL  点击和清理可以去调
        //设置显示通知时的默认的发声、震动、Light效果
        mNotification.defaults = Notification.DEFAULT_VIBRATE;
        //设置发出消息的内容
        mNotification.tickerText = "通知来了";
        //设置发出通知的时间
        mNotification.when = System.currentTimeMillis();
//		mNotification.flags = Notification.FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知
//		mNotification.setLatestEventInfo(this, "常驻测试", "使用cancel()方法才可以把我去掉哦", null); //设置详细的信息  ,这个方法现在已经不用了
        //显示
        notificationManager.notify(Constants.NOTIFICATION_ID_LONG, mNotification);
    }

    @Override
    public void notifyByRemoteViews() {
//        //开启通知服务:Notification管理
//        NotificationManager notificationManager = (NotificationManager) mAppActivity.getSystemService(NOTIFICATION_SERVICE);
//        PendingIntent pendingIntent = PendingIntent.getActivity(mAppActivity, 1, new Intent(), 0);
//        NotificationCompat.Builder builder = new NotificationCompat.Builder(mAppActivity);
//        //初始化RemoteViews
//
//        builder.setSmallIcon(R.mipmap.login_user);

        mSeniorView.toastShow(mAppActivity.getString(R.string.function_no));


    }

    @Override
    public void notifyClear() {
        NotificationManager notificationManager = (NotificationManager) mAppActivity.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(Constants.NOTIFICATION_ID_LONG);
    }

    @Override
    public void notifyClearAll() {
        NotificationManager notificationManager = (NotificationManager) mAppActivity.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
    }

    @Override
    public void showAlertProcess() {
        mSeniorView.alertShow(mAppActivity.getString(R.string.button_process_what),mAppActivity.getString(R.string.process_message));
    }

    @Override
    public void showAlertThread() {
        mSeniorView.alertShow(mAppActivity.getString(R.string.button_thread_what),mAppActivity.getString(R.string.thread_message));
    }

    @Override
    public void showAlertSummary() {
        mSeniorView.alertShow(mAppActivity.getString(R.string.summary),mAppActivity.getString(R.string.process_thread_message));
    }


    @Override
    public List<ItemBean> listGetItemData(boolean isRefresh) {
        int page = isRefresh ? 1 : mSeniorModel.getPage();
        List<ItemBean> itemBeanList = new ArrayList<>();
        itemBeanList.addAll(mSeniorModel.getItemBeanList(page));
        mSeniorModel.setPage(page + 1);
        return itemBeanList;
    }

    @Override
    public void setPullRefreshViewChild(int position) {
        mSeniorModel.setPullRefreshViewChild(position);
    }

    @Override
    public int getPullRefreshViewChild() {
        return mSeniorModel.getPullRefreshViewChild();
    }

}
